/**
 * 2016，thor.com，all rights reserved。
 * 
 * Project：		palmmall-core
 * Filename：	AUserServiceImpl.java
 * Created：		2016年7月30日
 * Creator：		wolv
 */
package com.thor.palmmall.rest.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.entity.UCN;
import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.query.QueryResult;
import com.thor.palmmall.dao.option.Option;
import com.thor.palmmall.dao.option.OptionDao;
import com.thor.palmmall.rest.common.ACodeNameQueryParam;
import com.thor.palmmall.rest.common.SCodeNameQueryParamConverter;
import com.thor.palmmall.rest.user.converter.ATenantUserConverter;
import com.thor.palmmall.rest.user.converter.AUserConverter;
import com.thor.palmmall.rest.user.converter.SAuthenInfoConverter;
import com.thor.palmmall.rest.user.converter.SPasswordInfoConverter;
import com.thor.palmmall.spi.common.SQueryResult;
import com.thor.palmmall.spi.common.SResponse;
import com.thor.palmmall.spi.process.SProcessNameConfig;
import com.thor.palmmall.spi.process.SProcessParam;
import com.thor.palmmall.spi.process.SProcessService;
import com.thor.palmmall.spi.user.STenantUser;
import com.thor.palmmall.spi.user.SUser;
import com.thor.palmmall.spi.user.SUserService;

/**
 * @author wolv
 *
 */
public class AUserServiceImpl implements AUserService {

  @Override
  public ATenantUser tenantLogin(AAuthenInfo authen) throws ThorServiceException {
    SResponse<STenantUser> sResponse = sUserService
        .tenantLogin(SAuthenInfoConverter.getInstance().convert(authen));
    if (sResponse.isSuccess()) {
      ATenantUser result = ATenantUserConverter.getInstance().convert(sResponse.getBody());

      filterPermission(result);

      return result;
    } else
      throw new ThorServiceException(sResponse.getMessage());
  }

  @Override
  public AUser marketLogin(AAuthenInfo authen) throws ThorServiceException {
    SResponse<SUser> sResponse = sUserService
        .marketLogin(SAuthenInfoConverter.getInstance().convert(authen));
    if (sResponse.isSuccess()) {
      AUser result = AUserConverter.getInstance().convert(sResponse.getBody());

      filterPermission(result);

      return result;
    } else
      throw new ThorServiceException(sResponse.getMessage());
  }

  @Override
  public void changePassword(String id, APasswordInfo password) throws ThorServiceException {
    sUserService.changePassword(id, SPasswordInfoConverter.getInstance().convert(password));
  }

  @Override
  public QueryResult<UCN> query(ACodeNameQueryParam param) throws ThorServiceException {
    SResponse<SQueryResult<UCN>> sResponse = sUserService
        .query(SCodeNameQueryParamConverter.getInstance().convert(param));
    if (sResponse.isSuccess()) {
      QueryResult<UCN> result = new QueryResult<UCN>();
      result.setPage(sResponse.getBody().getPage());
      result.setPageSize(sResponse.getBody().getPageSize());
      result.setPageCount(sResponse.getBody().getPageCount());
      result.setRecordCount(sResponse.getBody().getRecordCount());
      result.setRecords(sResponse.getBody().getRecords());
      return result;
    } else
      throw new ThorServiceException(sResponse.getMessage());
  }

  private void filterPermission(AUser user) throws ThorServiceException {
    assert user != null;

    SProcessParam param = new SProcessParam();
    param.setUserId(user.getCode());
    param.setPrefixs(SProcessNameConfig.PROCESS_PREFIX_LIST);
    SResponse<List<String>> resp = sProcessService.allStartProcesses(param);
    if (resp.isSuccess() == false)
      throw new ThorServiceException("获取流程权限出错..");

    List<String> processPermissions = resp.getBody();

    // 用户没有新建投诉单的流程权限
    if (processPermissions
        .contains(SProcessNameConfig.getInstance().getComplaintProcess()) == false) {
      user.getPermissions().remove(SUser.Permissions.PERM_COMPLAINADVICE_CREATE);
    }
    // 用户没有新建报修单的流程权限
    if (processPermissions
        .contains(SProcessNameConfig.getInstance().getMaintainProcess()) == false) {
      user.getPermissions().remove(SUser.Permissions.PERM_PROJECTREPAIR_CREATE);
    }
    // 用户没有新建销售录入的流程权限
    if (processPermissions
        .contains(SProcessNameConfig.getInstance().getSalesInputProcess()) == false) {
      user.getPermissions().remove(SUser.Permissions.PERM_SALEINPUT_CREATE);
    }
    // 用户没有新建设备巡检单的流程权限
    if (processPermissions
        .contains(SProcessNameConfig.getInstance().getDeviceInspectProcess()) == false) {
      user.getPermissions().remove(SUser.Permissions.PERM_DEVICEINSPECT_CREATE);
    }
    // 用户没有新建安保设备维修单的流程权限
    if (processPermissions
        .contains(SProcessNameConfig.getInstance().getDeviceRepairProcess()) == false) {
      user.getPermissions().remove(SUser.Permissions.PERM_DEVICEREPAIR_CREATE);
    }
    // 用户没有新建运营巡检单的流程权限
    if (processPermissions
        .contains(SProcessNameConfig.getInstance().getOperInspectProcess()) == false) {
      user.getPermissions().remove(SUser.Permissions.PERM_OPERINSPECT_CREATE);
    }
  }
  
  @Override
  public String getAttachments(String enterprise) throws ThorServiceException {
    Option option = optionDao.getOption(enterprise, "attachment");
    if(option != null){
      return option.getOptionValue();
    }
    return "";
  }
  
  @Override
  public String getOptionByStore(String enterprise) throws ThorServiceException {
    Option option = optionDao.getOption(enterprise, "confirmType");
    if(option != null){
      return option.getOptionValue();
    }
    return "";
  }



  @Autowired
  private SUserService sUserService;

  @Autowired
  private SProcessService sProcessService;
  
  @Autowired
  private OptionDao optionDao;
}
